#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
@Project ：pythonProject2 
@File    ：ex1.py
@Author  ：beibei
@Date    ：2023/12/22 3:30 PM 
'''
import socket
import time


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # SOCK_STREAM TCP的套接字  UDP SOCK_DGRAM
server_address = ("127.0.0.1", 8991)
server_socket.bind(server_address)
server_socket.listen(1)
print('Waiting for client connection...')

while True:
    client_socket, client_address = server_socket.accept()  # 和客户端建立通信通道
    print(f'client_address: {client_address}')
    init_time = int(time.time())
    while True:
        if int(time.time()) - init_time > 5:
            break
        try:
            client_socket.settimeout(1)
            data = client_socket.recv(1024).decode('utf-8')
        except TimeoutError:
            continue
        print(f'data: {data}')
        if data == 'close':
            break
        if data == '吴亦凡':
            msg = '他在踩缝纫机'
        elif data == '蔡徐坤':
            msg = '塌方了'
        elif data == '':
            continue
        else:
            msg = '目前没有消息'
        client_socket.sendall(msg.encode('utf-8'))
        init_time = int(time.time())
    client_socket.close()